# define variable
openresty_package=openresty-1.19.9.1
openresty_package_tar=${openresty_package}.tar.gz
PREFIX=/usr/local/openresty

# Install dependent development components
yum install wget gcc-c++ libtool gmake make -y
yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel readline readline-devel -y

# add user
grep nginx /etc/group &> /dev/null  || groupadd -r nginx
id nginx  &> /dev/null || useradd -r -M -g nginx -c "create-time is `date +%Y/%m/%d` ;is web service" -s /sbin/nologin nginx
echo 'ckeck user succeed'

# download software
cd /usr/local/src
test -f ${openresty_package_tar} || wget https://openresty.org/download/${openresty_package_tar}
test -d ${openresty_package} && rm -rf /usr/local/src/${openresty_package}
test -f ${openresty_package_tar} && tar xf ${openresty_package_tar} || exit 6
test -x ${openresty_package}/configure || exist 12 

cd ${openresty_package}/ && ./configure \
--prefix=${PREFIX} \
--sbin-path=${PREFIX}/nginx/sbin/nginx \
--conf-path=${PREFIX}/nginx/conf/nginx.conf \
--pid-path=${PREFIX}/nginx/logs/nginx.pid \
--error-log-path=${PREFIX}/nginx/logs/error.log \
--http-log-path=${PREFIX}/nginx/logs/access.log \
--user=nginx \
--group=nginx \
--with-pcre \
--with-pcre-jit \
--with-stream \
--with-threads \
--with-file-aio \
--with-luajit \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module

if [[  $? == 0 ]];then
    if gmake -v &> /dev/null;then
        gmake && gmake install
    else
        make $$ make install
    fi
else 
    echo 'configure failed. Exit immediately' && exit 8
fi


# add profile
test -f /etc/profile.d/openresty.sh || echo 'export PATH=/usr/local/openresty/bin:$PATH' > /etc/profile.d/openresty.sh
source /etc/profile

# delete installtion package
cd ../ && test -d ${openresty_package} && rm -rf /usr/local/src/${openresty_package}


# start service

